﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MouseCombo
{
    public partial class Form1 : Form
    {
        #region//定义变量

        const int AnimationCount = 80;
        private Point endPosition;//保存鼠标坐标
        private int count = 0;//点击次数
        private int intCounts = 1;//总次数，默认1次
        private decimal decSeconds = 1;//间隔秒数，默认1秒
        private bool flg = true;//控制循环
        private bool boolCounts;//是否有次数限制
        readonly KeyboardHook k_hook; //键盘全局监听代码
        #endregion
        public Form1()
        {
            InitializeComponent();
            //实例KeboardHook，并启动
            k_hook = new();
            k_hook.KeyDownEvent += K_hook_KeyDownEvent;
            k_hook.Start();
        }

        private void K_hook_KeyDownEvent(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                k_hook.Stop();
            }
            if (e.KeyCode == Keys.F8)
            {
                if (flg)
                {//如果没有运行
                    flg = false;
                    //点击次数
                    _=int.TryParse(txtCounts.Text, out intCounts);
                    boolCounts = intCounts > 0;
                    //间隔时间，默认1秒
                    _ = decimal.TryParse(cBoxSeconds.Text, out decSeconds);
                    timer1.Interval = decSeconds==0? 
                        1000 :Convert.ToInt32(decSeconds * 1000);
                    timer1.Start();
                    toolSSLStatusNow.Text = "正在运行";
                }
                else
                {//如果已运行
                    timer1.Stop();
                    flg = true;
                    toolSSLStatusNow.Text = "结束运行";
                }               
            }
        }
        #region 
        //结构体布局 本机位置

        [StructLayout(LayoutKind.Sequential)]

        struct NativeRECT

        {

            public int left;

            public int top;

            public int right;

            public int bottom;

        }



        //将枚举作为位域处理

        [Flags]

        enum MouseEventFlag : uint //设置鼠标动作的键值

        {

            Move = 0x0001, //发生移动

            LeftDown = 0x0002, //鼠标按下左键

            LeftUp = 0x0004, //鼠标松开左键

            RightDown = 0x0008, //鼠标按下右键

            RightUp = 0x0010, //鼠标松开右键

            MiddleDown = 0x0020, //鼠标按下中键

            MiddleUp = 0x0040, //鼠标松开中键

            XDown = 0x0080,

            XUp = 0x0100,

            Wheel = 0x0800, //鼠标轮被移动

            VirtualDesk = 0x4000, //虚拟桌面

            Absolute = 0x8000

        }



        //设置鼠标位置

        [DllImport("user32.dll")]

        static extern bool SetCursorPos(int X, int Y);



        //设置鼠标按键和动作

        [DllImport("user32.dll")]

        static extern void mouse_event(MouseEventFlag flags, int dx, int dy,

        uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型



        [DllImport("user32.dll")]

        static extern IntPtr FindWindow(string strClass, string strWindow);



        //该函数获取一个窗口句柄,该窗口雷鸣和窗口名与给定字符串匹配 hwnParent=Null从桌面窗口查找

        [DllImport("user32.dll")]

        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,

        string strClass, string strWindow);



        [DllImport("user32.dll")]

        static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);

        #endregion
        //单击执行
        private void Moudown()
        {
            SetCursorPos(endPosition.X, endPosition.Y);
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8 && k_hook.hookStatus == KeyboardHook.HookStatus.STOP)
            {
                k_hook.Start();
                K_hook_KeyDownEvent(sender, e);
            }
            if (e.KeyCode == Keys.Escape)
            {
                k_hook.Stop();
            }
        }
        private void Timer1_Tick(object sender, EventArgs e)
        {
            Point screenPoint = Control.MousePosition;
            endPosition = new Point(screenPoint.X, screenPoint.Y);
            Moudown();
            if (boolCounts)
            {
                count++;
                if (count > intCounts)
                {
                    timer1.Stop();
                }
            }
            
        }
    }
}